package com.example.controller;

import com.example.enetity.Result;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/file")
//支持跨域
@CrossOrigin
public class FileUploadController {

    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) throws IOException {
        String filename = file.getOriginalFilename();
        filename =  UUID.randomUUID().toString()+filename.substring(filename.lastIndexOf("."));
        file.transferTo(new File("D:\\tools\\VSCode\\WorkSpace\\bigEvent\\public\\imgs\\"+filename));
        return Result.success("url访问地址","../public/imgs/"+filename);
    }
}
